/*
    This file is part of Starlight Engine.

    Starlight Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    Starlight Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Starlight Engine.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using StarlightEngine;

namespace StarlightEngine
{
    public class TextNode : Node
    {
        SpriteFont font;
        bool created = false;
        bool initSize = false;
        string text = "mary had a little lamb.";
        Vector2 size = Vector2.Zero;
        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                RecreateSize();
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TextNode"/> class.
        /// </summary>
        /// <param name="font">The font as in the FontManager name..</param>
        public TextNode(string fontName)
            : base()
        {
            font = FontManager.GetFont(fontName);
            created = true;

            RecreateSize();
        }

        void RecreateSize()
        {
            if (!created)
                return;
            else
            {
                size = font.MeasureString(text);
                if (initSize == false)
                    initSize = true;
            }
        }

        public TextNode()
        {
            font = FontManager.GetFont("Engine.Font");
        }

        public override void Draw()
        {
            Engine.SpriteBatch.DrawString(font, text, Position, DrawColor, Rotation, Origin, Scale, SpriteEffects.None, 0f);
            //TODO : Fix rectangle being made every draw.
            Rectangle = new Rectangle((int)this.Position.X, (int)this.Position.Y, (int)(size.X * Scale),(int)( size.Y * Scale));            
            base.Draw();
        }

        protected override void CreateRectangle()
        {
            RecreateSize();
            Rectangle = new Rectangle((int)Position.X, (int)Position.Y, (int)(size.X * Scale), (int)(size.Y * Scale));
        }
    }
}